const api = require("../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    tabCur: 0,          //选中的选项卡页
    tabItem:["工作中","已完成","已撤销"],

    pageNo:1,
    listData:[]
  },
  _loadListData(){
    if(this.data.pageNo > 0){
      let state = 1;
      if(this.data.tabCur == 1){
          state = 2;
      }else if(this.data.tabCur == 2){
          state = 4;
      }
      const _self = this;
      api.post(api.url.orderList,{
          state:state,
          pageNo:this.data.pageNo
      },(res)=>{
          if(res.data.code == 200){
              let listData = _self.data.listData || []
              listData = listData.concat(res.data.body.orderItemList);
              _self.setData({listData:listData})
          }else if(res.data.code == 202){
              _self.data.pageNo = -1;
          }else if(res.data.msg){
              my.showModal({
                content: res.data.msg,
                  showCancel:false
              })
          }else{
              my.showToast({
                content: '获取数据失败',
                icon:'error'
              })
          }
      })
    }
  },

  //切换选项卡列表
  btnSelect(e) {
    const index = e.currentTarget.dataset.id;
    this.setData({
        listData:[],
        tabCur: index,
        pageNo:1
    })
    this._loadListData()
  },

  //跳转到设备详情页
  toInfo(e){
    let index = e.currentTarget.dataset.index;
    let item = this.data.listData[index];
    if(item.businessType == 'CDB'){
        my.navigateTo({
            url:'/pages/order/cdb/cdb?tradeNo='+item.tradeNo
        })
    }else if(item.businessType == 'CDCZ'){
        my.navigateTo({
            url:'/pages/order/cdcz/cdcz?tradeNo='+item.tradeNo
        })
    }else{
        my.showToast({
          content: '暂不支持查看',
          icon:'none'
        })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
      this.setData({
        pageNo:1,
        listData:[]
      })
      setTimeout(()=>{
          this._loadListData();
          my.stopPullDownRefresh();
      },800)
  },

  //触底分页
  onScrolltolower(){
    this._loadListData();
  },
  onLoad() {
    this._loadListData();
  },
});
